(function(){
    // Set csrf
    $.ajaxSetup({ 
         beforeSend: function(xhr, settings) {
             function getCookie(name) {
                 var cookieValue = null;
                 if (document.cookie && document.cookie != '') {
                     var cookies = document.cookie.split(';');
                     for (var i = 0; i < cookies.length; i++) {
                         var cookie = jQuery.trim(cookies[i]);
                         // Does this cookie string begin with the name we want?
                     if (cookie.substring(0, name.length + 1) == (name + '=')) {
                         cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                         break;
                     }
                 }
             }
             return cookieValue;
             }
             if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
                 // Only send the token to relative URLs i.e. locally.
                 xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
             }
         } 
    });

    // 处理左侧菜单的高亮显示
    var handle_menu_hlight = function(){
        // bind click
        $('.sidebar .nav li:has(a)').bind('click', function(){
            var href = $(this).find('a').attr('href');
            $.cookie('last_clicked_sidebar_a', href, {'path': '/'});
        });
        $('.navbar-fixed-top a').bind('click', function(){
            $.removeCookie('last_clicked_sidebar_a', {'path': '/'});
        });
        // hilight menu
        var lcsa = $.cookie('last_clicked_sidebar_a');
        if (typeof lcsa !== 'undefined') {
            $('.sidebar .nav a[href="' + lcsa + '"]').parent().addClass('active');
        }
    }

    //bind close action
    $(function(){
        $('.info-box button.close').on('click', function(){
            $(this).parent().fadeOut();
        });
        $('.fancybox').fancybox();

        handle_menu_hlight()
    });
})();

var BTdash = {
    // dashboard misc function
    "misc": (function (){

        var URL_RE = /^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/;
        var is_url = function(s) {
            return URL_RE.test(s);
        }

        // 弹出提示
        var info = function (opt){
            var default_opt = {
                'message': '',
                'icon': '',
                'level': 'info',
                'auto_disappear': 0,
                'effect': 'slideDown'
            }
            var opt = $.extend(default_opt, opt)
            if (!opt.message) {
                return;
            };

            var div_alert = '<div class="alert alert-' + opt.level +' bt-alert">';
            if (opt.icon) {
                div_alert += '<i class="icon-' + opt.icon + '"></i> ';
            }    
            div_alert += '<button class="close" data-dismiss="alert">×</button>' + opt.message + '</div>';
            div_alert = $(div_alert);

            div_alert.hide().prependTo($('.info-box'))
            if (opt.effect === 'slideDown') {
                div_alert.slideDown();
            } 
            else {
                div_alert.show();
            }
            if (opt.auto_disappear > 0) {
                div_alert.delay(opt.auto_disappear).slideUp();
            }

            div_alert.affix();
        };

        return {
            "is_url": is_url,
            "info": info
        }
    })()
};


// Add serializeObject function
(function($){
	$.fn.serializeObject = function () {
		"use strict";

		var result = {};
		var extend = function (i, element) {
			var node = result[element.name];

	// If node with same name exists already, need to convert it to an array as it
	// is a multi-value field (i.e., checkboxes)

			if ('undefined' !== typeof node && node !== null) {
				if ($.isArray(node)) {
					node.push(element.value);
				} else {
					result[element.name] = [node, element.value];
				}
			} else {
				result[element.name] = element.value;
			}
		};

		$.each(this.serializeArray(), extend);
		return result;
	};
})(jQuery);

